package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author GongXincheng
 * @since 2019-09-17 15:19
 */
public class JdkProxyMain {

    private static RealSubject realSubject;

    public static void main(String[] args) {
        Subject proxySubject = (Subject) Proxy.newProxyInstance(
                JdkProxyMain.class.getClassLoader(),
                RealSubject.class.getInterfaces(),
                (proxy, method, args1) -> {
                    if (realSubject == null) {
                        realSubject = new RealSubject();
                    }
                    return method.invoke(realSubject, args1);
                });

        proxySubject.speak("Hello World !");
    }

}
